package com.susem.mw.servlets;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jbpm.api.TaskService;

import com.susem.mw.util.ThesisUtils;

/**
 * Servlet implementation class DownloadThesisServlet
 */
public class DownloadThesisServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DownloadThesisServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			TaskService ts = ThesisUtils.PROCESS_ENGINE.getTaskService();
			String taskId = request.getParameter("taskId");
			String fileName = (String) ts.getVariable(taskId, "fileName");
			byte[] content = (byte[]) ts.getVariable(taskId, "document");
			response.reset();
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment; filename=\""  + fileName + "\"");
			OutputStream w = response.getOutputStream();
			w.write(content);
			w.close();
		}
		catch (Exception e)
		{

		} 
	}

}
